/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.ICommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.RegisterService;
import com.lmx.persistence.service.UserSessionService;
import com.lmx.repoDomain.tmodel.UserSession;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(UserSession.class)
@Component
public class UserSessionRepository extends BaseRepository<UserSession> {
	private Log logger = LogFactory.getLog(this.getClass());
	@Autowired
	protected UserSessionService userSessionService;
	@Autowired
	private RegisterService registerService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(UserSession.class);
	}

	@Override
	@JsonApiFindAll
	public Iterable<UserSession> findAll(final QueryParams requestParams) {
		Map<String, String> map = CommonUtil.getKeyMapByFilter(requestParams);
		if (map.get("bind") != null) {
			// 公众号绑定
			String name = map.get("name");
			String birthday = map.get("birth");
			String code = map.get("code");
			Integer date = null;
			try {
				date = CommonUtil.strToIntegerTime(CommonUtil
						.integetTimeForString(Integer.parseInt(birthday))
						.substring(0, 10)
						+ " 08:00:00");
			} catch (Exception e) {

			}
			return userSessionService.bindCustomer(name, date.toString(), code);
		} else {
			return userSessionService.login(requestParams);
		}

	}

	@Override
	@JsonApiSave
	public UserSession save(UserSession userSession) {
		String action = ActionContextHolder.getContext().getAction();
		if (userSession.getId() == null) {
			if (StringUtils.isNotBlank(action) && "reg".equals(action)) {
				logger.debug("action reg");
				userSession = registerService.register(userSession);// 注册
			}
			if (StringUtils.isNotBlank(action)
					&& "resetPassword".equals(action)) {
				logger.debug("action resetPassword");
				userSession = registerService.resetPassword(userSession);// 重置密码
			}

		} else {
			if (StringUtils.isNotBlank(action)
					&& "modifyPassword".equals(action)) {
				logger.debug("action modifyPassword");
				userSession = registerService.modifyPassword(userSession);// 修改密码
			} else {
				userSession
						.setErrcode(ICommonConstants.INTERFACEERRO.NOINTERFACE
								.ordinal());
			}

		}
		return userSession;
	}

}
